package com.lebang.configuration.datasource;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 数据源AOP切面定义
 *
 * @author: hwq
 * @date:
 */
@Component
@Aspect
@Slf4j
public class DataSourceAspect {


    //切入点在service层的方法上，配置aop的切入点
    @Pointcut("@annotation(DataSource)")
    public void dataSourcePointCut() {
    }



    @Before("dataSourcePointCut()")
    public void preHandle(JoinPoint joinPoint) {
        DynamicDataSourceHolder.putDataSouce("commonDB");
    }


    //执行完切面后，恢复默认数据源
    @After("dataSourcePointCut()")
    public void after(JoinPoint joinPoint){
        DynamicDataSourceHolder.removeDataSource();
        //DynamicDataSourceHolder.putDataSouce("defaultDB");
    }

}